const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        islog:false,
        isnolog:false,
        log:'',
        logyes:'',
        logitem:'',
        logindex:'',
        logtype:4,
        logfalse:true,
        statustitle:'跟进完成',
        is_model:2,  //1列表模式 2用户详情
        consultant_id:'',
        gradelist:[],
        followlist:[{title:'全部',id:''},{title:'待联系',id:1},{title:'需再次跟进',id:2},{title:'跟进完成',id:3},{title:'不感兴趣',id:4}],
        id:'',
        serchobj:{
            consultant_id:'',//	是	string	consultant_id
            id:'',//	是	string	列表id
            keyword:'',//	否	string	keyword
            grade:'',//	否	string	grade
            follow_type:'',//	否	string	follow_type 1 待联系 2需再次跟进 3跟进完成 4不感兴趣
            page:1,//	否	string	page
            limit:1,//	否	string	limit
        },
        userdata:[],
        errcode:-1,
        isshow:1,
        searre:'全部数据(按序)',
    },
    nonesho(){
        var that = this;
        that.setData({
            isshow:1,
        })
    },
    nofollow(){
        var that = this;
        that.setData({
            isnolog:false,
            islog:false,
        })
    },
    followlogclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        var type = e.currentTarget.dataset.type || e.target.dataset.type;
        var index = e.currentTarget.dataset.index;
        that.setData({
            logitem:item,
            logtype:type,
            logindex:index
        })
        if(Number(type)==4){
            that.setData({
                isnolog:true,
            })
        }else{
            if(Number(type)==2){
                that.setData({
                    statustitle:'需再次跟进',
                    islog:true,
                })
            }else{
                that.setData({
                    statustitle:'跟进完成',
                    islog:true,
                })
            }
        }
    },
    searchclfollwbutick(e){
        var that = this;
        var log = e.detail.value.log;
        if(log==''){
            wx.showToast({
                title: '请输入跟进记录',
                icon: 'none',
            })
        }else{
            that.setData({
                logyes: log,
            })
            that.followLog();
        }
    },
    getNowDate(){
        var timestamp = Date.parse(new Date());
        var date = new Date(timestamp);
        //获取年份  
        var Y =date.getFullYear();
        //获取月份
        var M =date.getMonth()+1;
        //获取当前天
        var D = date.getDate();
        //获取当前小时
        var H = date.getHours()
        var n= date.getMinutes()
        var s = date.getSeconds()
        return + Y + '-' + M +'-' + D +' ' + H+':'+n+'';
    },
    followLog(){
        var that = this;
        if(that.data.logfalse){
            that.setData({
                logfalse:false
            })
            wx.showLoading({
                title: '加载中~',
            })
            if(Number(that.data.logtype)!=4){
                var log = that.data.logyes;
            }else{
                var log = '';
            }
            var objdata = {
                customer_id:that.data.logitem.id,//	是	string	用户列表id
                consultant_id:that.data.consultant_id,//	是	string	consultant_id
                log:log,//	是	string	
                follow_type:that.data.logtype,//	是	string	2需再次跟进 3跟进完成 4不感兴趣
            }
            share.wxRequest('crmapi/DataHandling/followLog',objdata,function(res){
                if (Number(res.data.errcode) == 0) {
                    wx.showToast({
                        title: '成功',
                        icon: 'none',
                    })
                    that.setData({
                        isnolog:false,
                        islog:false,
                    })
                    // logindex
                    if(Number(that.data.is_model)==1){
                        var item = that.data.logitem;
                        item.follow_type = Number(that.data.logtype);
                        var index = that.data.logindex;
                        var objdata = [{
                            addtime: that.getNowDate(),
                            follow_type:Number(that.data.logtype),
                            log: "",
                        }]
                        if(Number(that.data.logtype)!=4){
                            objdata[0].log = log
                        }
                        item.log = objdata.concat(item.log);
                        that.data.userdata[index] = item;
                        that.setData({
                            userdata:that.data.userdata
                        })
                    }else{
                        that.userList();
                    }
                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    })
                }
                that.setData({
                    logfalse:true
                })
            })
        }
    },
    changemode(){
        var that = this;
        var is_model = that.data.is_model;
        if(Number(is_model)==1){
            var is_model1 = 2;
            var limit = 1;
        }else{
            var is_model1 = 1;
            var limit = 10;
        }
        that.setData({
            is_model:is_model1,
            'serchobj.limit':limit,
            'serchobj.page':1,
            userdata:[],
        })
        that.userList();
    },
    searchclick(e){
        var that = this;
        var keyword = e.detail.value.keykown;
        that.setData({
            'serchobj.keyword': keyword,
        })
        that.userList();
    },
    changeshe(){
        var that = this;
        that.setData({
            'serchobj.page':1,
            isshow:2,
        })
    },
    copyphone(e){
        var that = this;
        var phone = e.currentTarget.dataset.phone || e.target.dataset.phone;
        wx.setClipboardData({
            data: phone,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                })
            },
        })
    },
    itemchane(e){
        var that = this;
        if(Number(that.data.is_model)==1){
            var item = e.currentTarget.dataset.item || e.target.dataset.item;
            var index = e.currentTarget.dataset.index;
            var userdata = that.data.userdata;
            userdata[index].decfalse = !item.decfalse;
            that.setData({
                userdata:userdata
            })
        }
    },
    gradeclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            'serchobj.grade':item.grade
        })

    },

    followclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            'serchobj.follow_type':item.id
        })
    },

    // 点击确定

    sureclick(){
        var that = this;
        var serchobj = that.data.serchobj,follow='',grade='';
        if(Number(serchobj.follow_type)==1){
            follow = '待联系';
        }else if(Number(serchobj.follow_type)==2){
            follow = '需再次跟进';
        }else if(Number(serchobj.follow_type)==3){
            follow = '跟进完成';
        }else if(Number(serchobj.follow_type)==4){
            follow = '不感兴趣';
        }
        if(serchobj.grade!=''){
            grade = serchobj.grade;
        }
        if(follow!=''&&grade!=''){
            var title = '质量:'+ grade + ',状态:'+ follow 
        }else if(follow!=''){
            var title ='质量:全部,状态:'+ follow 
        }else if(grade!=''){
            var title = '质量:'+ grade  + ',状态:全部'
        }else{
            var title = '全部数据(按序)'
        }
        that.setData({
            'serchobj.page':1,
            isshow:1,
            searre:title
        })
        that.userList();
    },

    // 点击重置
    closech(){
        var that = this;
        if(Number(that.data.is_model) == 1){
            var limit = 10;
        }else{
            var limit = 1;
        }
        that.setData({
            serchobj:{
                consultant_id:that.consultant_id,//	是	string	consultant_id
                id:that.id,//	是	string	列表id
                keyword:'',//	否	string	keyword
                grade:'',//	否	string	grade
                follow_type:'',//	否	string	follow_type 1 待联系 2需再次跟进 3跟进完成 4不感兴趣
                page:1,//	否	string	page
                limit:limit,//	否	string	limit
            },
            isshow:1,
            searre:'全部数据(按序)'
        })
        that.userList();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            consultant_id:options.consultant_id,
            id:options.id,
        })
        that.getGrade();
        that.userList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },
    userList(){
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        that.setData({
            'serchobj.consultant_id':that.data.consultant_id,
            'serchobj.id':that.data.id,
        })
        share.wxRequest('crmapi/DataHandling/userList',that.data.serchobj,function(res){
            if (Number(res.data.errcode) == 0) {
                var data = res.data.lists;
                for(var i=0;i<data.length;i++){
                    data[i]['phone_chen'] = ''
                    data[i]['arrall'] = [];
                    if(Number(that.data.serchobj.limit)==1){
                        data[i]['decfalse'] = true;
                    }else{
                        data[i]['decfalse'] = false;
                    }
                    if(data[i].tel&&data[i].tel!=''){
                        var tel = data[i].tel;
                        data[i].phone_chen = tel.substring(0, 3)+"****"+tel.substr(tel.length-4);
                    }
                    var objTK = {
                        title:'题库',
                        list:[]
                    }
                    if(data[i].tiku){
                        objTK.list = data[i].tiku.split(',')
                    }
                    data[i].arrall.push(objTK)
                    var objAc = {
                        title:'活动',
                        list:[]
                    }
                    if(data[i].activity){
                        objAc.list = data[i].activity.split(',')
                    }
                    data[i].arrall.push(objAc)
                    var objdr = {
                        title:'引流课',
                        list:[]
                    }
                    if(data[i].drainage){
                        objdr.list = data[i].drainage.split(',')
                    }
                    data[i].arrall.push(objdr)
                    var objms = {
                        title:'面授-引流课',
                        list:[]
                    }
                    if(data[i].ms_drainage){
                        objms.list = data[i].ms_drainage.split(',')
                    }
                    data[i].arrall.push(objms)
                    var objmians = {
                        title:'面授课',
                        list:[]
                    }
                    if(data[i].mianshou){
                        objmians.list = data[i].mianshou.split(',')
                    }
                    data[i].arrall.push(objmians)
                    var objwk = {
                        title:'网课',
                        list:[]
                    }
                    if(data[i].wangke){
                        objwk.list = data[i].wangke.split(',')
                    }
                    data[i].arrall.push(objwk)
                    var objbooks = {
                        title:'网课',
                        list:[]
                    }
                    if(data[i].books){
                        objbooks.list = data[i].books.split(',')
                    }
                    data[i].arrall.push(objbooks)
                }
                if(Number(that.data.serchobj.page)==1){
                    that.setData({
                        userdata:data,
                    })
                }else{
                    that.setData({
                        userdata:that.data.userdata.concat(data),
                    })
                }
            }
            that.setData({
                errcode:1,
            })
            wx.hideLoading();
        })
    },

    getGrade(){
        var that = this;
        share.wxRequest('crmapi/DataHandling/getGrade',{id:that.data.id},function(res){
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj=[{grade:''}]
                that.setData({
                    gradelist:obj.concat(data),
                })
            }
        })
    },



    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if(Number(that.data.is_model)==1){
            var page = Number(that.data.serchobj.page) + 1;
            that.setData({
                'serchobj.page':page
            })
            that.userList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})